<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="100px">
      <el-form-item label="版本号" prop="versionCode">
        <el-input v-model="dataForm.versionCode" placeholder="请输入版本号，如：1.0.0"></el-input>
      </el-form-item>
      <el-form-item label="版本名称" prop="versionName">
        <el-input v-model="dataForm.versionName" placeholder="请输入版本名称"></el-input>
      </el-form-item>
      <el-form-item label="版本描述" prop="versionDesc">
        <el-input type="textarea" v-model="dataForm.versionDesc" :rows="4" placeholder="请输入版本描述"></el-input>
      </el-form-item>
      <el-form-item label="下载地址" prop="downloadUrl">
        <el-input v-model="dataForm.downloadUrl" placeholder="请输入下载地址">
          <template slot="append">
            <el-upload
              class="upload-demo"
              :action="uploadUrl"
              :on-success="handleUploadSuccess"
              :on-error="handleUploadError"
              :show-file-list="false">
              <el-button size="small" type="primary">上传</el-button>
            </el-upload>
          </template>
        </el-input>
      </el-form-item>
      <el-form-item label="强制更新" prop="forceUpdate">
        <el-switch
          v-model="dataForm.forceUpdate"
          active-color="#ff4949"
          inactive-color="#13ce66">
        </el-switch>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-radio-group v-model="dataForm.status">
          <el-radio :label="1">已发布</el-radio>
          <el-radio :label="0">未发布</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="发布日期" prop="releaseDate">
        <el-date-picker
          v-model="dataForm.releaseDate"
          type="datetime"
          placeholder="选择发布日期"
          value-format="yyyy-MM-dd HH:mm:ss">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="创建人" prop="creator">
        <el-input v-model="dataForm.creator" placeholder="请输入创建人"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  data () {
    return {
      visible: false,
      dataForm: {
        id: 0,
        versionCode: '',
        versionName: '',
        versionDesc: '',
        downloadUrl: '',
        forceUpdate: false,
        status: 0,
        releaseDate: '',
        creator: ''
      },
      dataRule: {
        versionCode: [
          { required: true, message: '版本号不能为空', trigger: 'blur' }
        ],
        versionName: [
          { required: true, message: '版本名称不能为空', trigger: 'blur' }
        ],
        versionDesc: [
          { required: true, message: '版本描述不能为空', trigger: 'blur' }
        ],
        downloadUrl: [
          { required: true, message: '下载地址不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '状态不能为空', trigger: 'change' }
        ],
        releaseDate: [
          { required: true, message: '发布日期不能为空', trigger: 'change' }
        ],
        creator: [
          { required: true, message: '创建人不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (id) {
      this.dataForm.id = id || 0
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.$http({
            url: this.$http.adornUrl(`/algorithmVersion/algorithmVersion/queryWormholeGalaxyAlgorithmVersion`),
            method: 'post',
            data: this.$http.adornData({
              "id":id
            })
          }).then(({data}) => {
            if (data && data.success) {
              this.dataForm.versionCode = data.data.versionCode
              this.dataForm.versionName = data.data.versionName
              this.dataForm.versionDesc = data.data.versionDesc
              this.dataForm.downloadUrl = data.data.downloadUrl
              this.dataForm.forceUpdate = data.data.forceUpdate
              this.dataForm.status = data.data.status
              this.dataForm.releaseDate = data.data.releaseDate
              this.dataForm.creator = data.data.creator
            }
          })
        }
      })
    },
    // 上传成功回调
    handleUploadSuccess(response, file) {
      if (response && response.success) {
        this.dataForm.downloadUrl = response.data.url
        this.$message.success('上传成功')
      } else {
        this.$message.error(response.msg || '上传失败')
      }
    },
    // 上传失败回调
    handleUploadError() {
      this.$message.error('上传失败')
    },
    // 表单提交
    dataFormSubmit () {
      this.$refs['dataForm'].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/algorithmVersion/algorithmVersion/${!this.dataForm.id ? '/createWormholeGalaxyAlgorithmVersion' : 'createWormholeGalaxyAlgorithmVersion'}`),
            method: 'post',
            data: this.$http.adornData({
              'id': this.dataForm.id || undefined,
              'versionCode': this.dataForm.versionCode,
              'versionName': this.dataForm.versionName,
              'versionDesc': this.dataForm.versionDesc,
              'downloadUrl': this.dataForm.downloadUrl,
              'forceUpdate': this.dataForm.forceUpdate,
              'status': this.dataForm.status,
              'releaseDate': this.dataForm.releaseDate,
              'creator': this.dataForm.creator
            })
          }).then(({data}) => {
            if (data && data.success) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.el-select {
  width: 100%;
}
.upload-demo {
  display: inline-block;
  margin-left: 10px;
}
</style>
